﻿using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Service.Auth.Api.Application.Command;
using Service.Auth.Api.Application.Query;
using Service.Auth.Model.Model.Dto;
using Service.Auth.Model.Model.QueryDto;
using Service.Auth.Model.Model.RequestDto.Role;
using Service.Core.Filters;
using Service.Core.Identity.Login;
using Service.Core.Identity.Model;
using Service.Framework.Packet;

namespace Service.Auth.Api.Controller
{

    /// <summary>
    /// 
    /// </summary>
    /// <param name="mediator"></param>
    /// <param name="loginUserSession"></param>
    /// <param name="query"></param>
    /// <param name="rolePemissionsQuery"></param>
    public class RoleController(IMediator mediator, ILoginUserSession loginUserSession, RoleQueryService query, RolePemissionsQueryService rolePemissionsQuery) : BaseController(mediator)
    {
        /// <summary>
        /// 创建角色
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [Authorize(Policy = AuthPolicy.Permission)]
        [OperateLogFilter("角色管理", "创建角色")]
        public async Task<bool> CreateRole([FromBody] CreateRoleDto dto
        )
        {
            var command = new CreateRoleCommand(dto);
            return await _mediator.Send(command);
        }

        /// <summary>
        /// 修改角色
        /// </summary>
        /// <returns></returns>
        [HttpPut]
        [Authorize(Policy = AuthPolicy.Permission)]
        [OperateLogFilter("角色管理", "修改角色")]
        public async Task<bool> UpdateRole([FromBody] UpdateRoleDto dto)
        {
            var command = new UpdateRoleCommand(dto);
            return await _mediator.Send(command);
        }

        /// <summary>
        /// 删除角色
        /// </summary>
        /// <returns></returns>
        [HttpDelete]
        [Authorize(Policy = AuthPolicy.Permission)]
        [OperateLogFilter("角色管理", "删除角色")]
        public async Task<bool> DeleteRole([FromBody] DeleteRoleDto dto)
        {
            var command = new DeleteRoleCommand(dto);
            return await _mediator.Send(command);
        }


        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<PagedList<RoleDto>> QueryRolePages([FromBody] QueryRolePageModel condition)
        {
            return await query.QueryRolePage(condition);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<RoleDto> QueryRoleById([FromQuery] QueryRoleById condition)
        {
            return await query.QueryRoleById(condition);
        }



        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<List<IdName<string, string>>> QueryRoleDropDownBySystemId([FromQuery] long SystemId)
        {
            var loginUserInfo = loginUserSession.UserInfo;
            return await query.QueryRoleDropDownBySystemId(SystemId, loginUserInfo.RoleMaxLevel);
        }


        /// <summary>
        /// 查询权限下拉框
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<Dictionary<long, List<IdName<long, string>>>> QueryRoleDropDown([FromQuery] QueryRoleDropDown condition)
        {
            return await query.QueryRoleDropDown(condition);
        }


        /// <summary>
        /// 角色绑定菜单
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [Authorize(Policy = AuthPolicy.Permission)]
        [OperateLogFilter("角色管理", "角色权限绑定")]
        public async Task<bool> RoleBindPermissions([FromBody] RoleBindPermissionsDto dto)
        {
            var command = new RoleBindPermissionsCommand(dto);
            return await _mediator.Send(command);
        }


        /// <summary>
        /// 角色绑定菜单
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<List<PermissionDto>> QueryRolePermissionsByRoleId([FromQuery] long id)
        {
            var rolePemissions = await rolePemissionsQuery.QueryRolePemissionByRoles(new List<long>() { id });
            return rolePemissions;
        }


    }
}